﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Person_Mobile
{
    public partial class MainPage : PhoneApplicationPage
    {
        public List<PersonService.Person> pList = new List<PersonService.Person>();
        public  DS_NET_Client ds = new DS_NET_Client();
        public  WindowsPhoneControl1 NewPanel = new WindowsPhoneControl1();
        public ListBox Listbox_persons = new ListBox();
        
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            Listbox_persons.Margin = new Thickness(0,0,0,0);
            NewPanel.Margin = new Thickness(1000, 0, 0, 0);
            Listbox_persons.Height = 519;
            Listbox_persons.Width = 433;
            Listbox_persons.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            Listbox_persons.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            Listbox_persons.DoubleTap += new EventHandler<GestureEventArgs>(Listbox_persons_DoubleTap);
            ContentPanel.Children.Add(Listbox_persons);
            ContentPanel.Children.Add(NewPanel);
            NewPanel.main = this;
            
            Load_Click(this, new RoutedEventArgs());
        }

        private void Load_Click(object sender, RoutedEventArgs e)
        {
            pList = ds.Load();
            Listbox_persons.Items.Clear();
            foreach (PersonService.Person p in pList)
            {
                if (p!=null)
                {
                   Listbox_persons.Items.Add("id: " + p.Id.ToString() + " First Name: " + p.FName);                   
                }
            }            
        }

        private void Btn_new_Click(object sender, RoutedEventArgs e)
        {            
            Listbox_persons.Margin = new Thickness(1000, 0, 0, 0);                 
            NewPanel.Margin = new Thickness(0, 0, 0, 0);
            NewPanel.command = "Create";
        }

        private void Btn_Delete_Click(object sender, RoutedEventArgs e)
        {
            string selected = Listbox_persons.SelectedValue.ToString();
            string[] temp = selected.Split(' ');
            int id = Convert.ToInt32(temp[1]);
            ds.Delete(id);
            System.Threading.Thread.Sleep(200);
            Load_Click(sender, e);
        }

        private void Listbox_persons_DoubleTap(object sender, RoutedEventArgs e)
        {
            Listbox_persons.Margin = new Thickness(1000, 0, 0, 0);
            NewPanel.Margin = new Thickness(0, 0, 0, 0);
            string selected = Listbox_persons.SelectedValue.ToString();
            string[] temp = selected.Split(' ');
            int id = Convert.ToInt32(temp[1]);

            PersonService.Person p = null;

            foreach (PersonService.Person pp in pList)
            {
                if (pp.Id == id)
                {
                    p = pp;
                    break;
                }
            }
            NewPanel.Textbox_id.Text = p.Id.ToString();
            NewPanel.TextBox_name.Text = p.FName;
            NewPanel.TextBox_add1.Text = p.LName;
            NewPanel.TextBox_add2.Text = p.Age.ToString();
            NewPanel.command = "Edit";
            NewPanel.oldID = id;
            NewPanel.ListContact.DataContext = p.Contacts;
            System.Threading.Thread.Sleep(200);
            Load_Click(sender, e);
        }
    }
}